home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 526-550 / disk_539 / rpn / source / error.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  4KB  |  146 lines

  1. /*----------------------------------------*
  2.  | File: ERROR.c - MLO 900131 V1.00       |
  3.  | These routines perform error handling  |
  4.  | for mathematical errors, as described  |
  5.  | in the Lattice C library reference     |
  6.  | manual; AND set MathError to True,     |
  7.  | displaying a requester about the error |
  8.  *----------------------------------------*/
  9.  
  10. #include "rpn.h"
  11. #include "herror.h"
  12. #include <error.h>
  13. #include <math.h>
  14. #include <signal.h>
  15.  
  16. extern int errno;
  17. extern int _FPERR;
  18. extern int (*_SIGFPE)();
  19. extern Boolean MathError;
  20. extern struct Window *Wrpn;
  21.  
  22. static void display(char *pc1, char *pc2);
  23.  
  24. int matherr(
  25.   struct exception *x
  26. )
  27. {/*------------------------------------*
  28.   | Error from high-level mathematical |
  29.   | functions. Determines the routine  |
  30.   | name and the error type, then      |
  31.   | displays a requester.              |
  32.   *------------------------------------*/
  33.  
  34.   char slate[SLATE_DIM];
  35.  
  36.   sprintf(slate, "routine \"%s\"", x->name);
  37.   switch (x->type) {
  38.     case DOMAIN:
  39.       errno = EDOM;
  40.       display(slate, "(domain error)");
  41.       break;
  42.     case SING:
  43.       errno = EDOM;
  44.       display(slate, "(singularity)");
  45.       break;
  46.     case OVERFLOW:
  47.       errno = ERANGE;
  48.       display(slate, "(floating overflow)");
  49.       break;
  50.     case UNDERFLOW:
  51.       errno = ERANGE;
  52.       display(slate, "(floating underflow)");
  53.       break;
  54.     case TLOSS:
  55.       errno = ERANGE;
  56.       display(slate, "(total loss of significance)");
  57.       break;
  58.     case PLOSS:
  59.       errno = ERANGE;
  60.       display(slate, "(partial loss of significance)");
  61.       break;
  62.     default:
  63.       errno = ERANGE;
  64.       display(slate, NULL);
  65.       break;
  66.   }
  67.   return 0;
  68. }
  69.  
  70. void __stdargs CXFERR(
  71.   int code
  72. )
  73. {/*-------------------------------------------------------*
  74.   | Error from low-level floating point operations.       |
  75.   | Determines the error type, then displays a requester. |
  76.   *-------------------------------------------------------*/
  77.  
  78.   static char llfp[] = "low-level floating operation";
  79.   
  80.   _FPERR = code;
  81.   if ((void *) _SIGFPE != (void *) SIG_DFL   &&
  82.       (void *) _SIGFPE != (void *) SIG_IGN)             (*_SIGFPE)(SIGFPE);
  83.   switch (code) {
  84.     case FPEUND:
  85.       display(llfp, "(floating underflow)");
  86.       break;
  87.     case FPEOVF:
  88.       display(llfp, "(floating overflow)");
  89.       break;
  90.     case FPEZDV:
  91.       display(llfp, "(division by zero)");
  92.       break;
  93.     case FPENAN:
  94.       display(llfp, "(not a number)");
  95.       break;
  96.     case FPECOM:
  97.       display(llfp, "(not comparable)");
  98.       break;
  99.     default:
  100.       display(llfp, NULL);
  101.       break;
  102.   }
  103. }
  104.  
  105. static void display(
  106.   char *pc1,                    /* First line to be displayed */
  107.   char *pc2                     /* Second line, or NULL */
  108. )
  109. {/*---------------------------------------------*
  110.   | Local function. Displays the requester with |
  111.   | an header followed by the given text. Also  |
  112.   | sets to True the global variable MathError. |
  113.   *---------------------------------------------*/
  114.  
  115.   struct IntuiText IT3 = {
  116.     BLUE_PEN, WHITE_PEN, JAM2, IT3X, IT3Y, NULL,
  117.     NULL, NULL
  118.   };
  119.  
  120.   struct IntuiText IT2 = {
  121.     BLUE_PEN, WHITE_PEN, JAM2, IT2X, IT2Y, NULL,
  122.     NULL, NULL
  123.   };
  124.  
  125.   struct IntuiText IT1 = {
  126.     BLUE_PEN, WHITE_PEN, JAM2, IT1X, IT1Y, NULL,
  127.     "Mathematical error in", NULL
  128.   };
  129.  
  130.   struct IntuiText ITOK = {
  131.     BLUE_PEN, WHITE_PEN, JAM2, ITOKX, ITOKY, NULL,
  132.     "OK", NULL
  133.   };
  134.  
  135.   IT1.NextText = &IT2;
  136.   IT2.IText = pc1;
  137.   if (pc2 != NULL) {
  138.     IT3.IText = pc2;
  139.     IT2.NextText = &IT3;
  140.   } else {
  141.     IT2.NextText = NULL;
  142.   }
  143.   AutoRequest(Wrpn, &IT1, NULL, &ITOK, 0, 0, ITWID, ITHEI);
  144.   MathError = True;
  145. }
  146.